﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EditDetailInfoPanel : AppUI
{

    public EditHeightAndWeightUI firstStepGo;
    public EditIconUI iconGo;

    UserDetailInfoData userDetailInfoData;

    int m_Step;

    public const int Step_First = 0;
    public const int Step_Second = 1;

    public static bool iconSetted => MGame.instance.MUser.userInfo.icon != null;
    public override void Start()
    {
        base.Start();
    }

    public override void DoOnActive()
    {
        base.DoOnActive();

        //GameDebug.Log($"EditDetailInfoPanel -> DoOnActive");

        Init();
    }

    void Init()
    {
        var muser = MGame.instance.MUser;

        int step = Step_First;
        //if (muser.userInfo.birthYear <= 0)
        //{
        //    step = Step_First;
        //}
        //else
        //{
        //    step = Step_Second;
        //}
        m_Step = step;
        ShowObj(m_Step);

        userDetailInfoData = new UserDetailInfoData();

        firstStepGo.onEditEnd = OnFirstStepEidtEnd;
        iconGo.onEditEnd = OnSecondStepEditEnd;
    }

    void OnFirstStepEidtEnd(float height, float weight, int maritalStatus, string hometownProvince,string hometownCity, string job)
    {
        userDetailInfoData.height = height;
        userDetailInfoData.weight = weight;
        userDetailInfoData.maritalStatus = maritalStatus;
        userDetailInfoData.hometownProvince = hometownProvince;
        userDetailInfoData.hometownCity = hometownCity;
        userDetailInfoData.job = job;

        if (iconSetted)
        {
            OnEnd();
        }
        else
        {
            m_Step++;
            ShowObj(m_Step);
        }
    }
    void OnSecondStepEditEnd(Tex icon)
    {
        userDetailInfoData.icon = icon;

        OnEnd();
    }

    void OnEnd()
    {
        PanelLoading.Show(5);
        RequestServerController.instance.ReqDetailInfo(userDetailInfoData, (ret) =>
        {
            PanelLoading.Hide();
            GoBack();
        });
    }

    void ShowObj(int step)
    {
        this.firstStepGo.gameObject.SetActive(step == Step_First);
        this.iconGo.gameObject.SetActive(step == Step_Second);
    }


    // Update is called once per frame
    void Update()
    {

    }

    public void GoBack()
    {
        var main = UIController.ShowUI<MainPanel>();
        main.Refresh();
    }

}
